//
//  E_invoce.m
//  pa
//
//  Created by HaiJiao Chen on 12-12-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "E_invoce.h"
#import "FilePack.h"
#import "MultiLanguageButton.h"
#import "XMLObject.h"
#import "ASIHTTPRequest.h"
#import "CHJAlertView.h"
#import "ListControllerBG.h"
#import "KXUIColor.h"
#import "TicketList.h"
#import "MainObject.h"
#include "text_def.h"

@implementation E_invoce
@synthesize table;
@synthesize email_address;

- (void)bind
{
    [email_list setupController:self.table data:self reuse:@"emails"];
    [self.email_address becomeFirstResponder];
    ListControllerBG* bg = [[ListControllerBG alloc] initWithFrame:CGRectZero];
    [email_list setBGWithColor1:[UIColor colorWithARGB:0xFFF7F7F7] color2:[UIColor colorWithARGB:0xFFC4CCD6] height:38 bg:bg];
    [bg release];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        email_list = [[EMailList alloc] init];
        email_list.selectedIndex = -1;
        email_list.delegate = self;
    }
    return self;
}
-(void)dealloc
{
    [email_list release];
    [super dealloc];
}
-(NSArray*)data
{
    TicketList* list = [AppDelegate sharedInstance].main.ticketsDataSource;
    NSDictionary* custom = [list.memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
    
    NSMutableArray* custom_emails = [custom valueForKey:CUSTOM_EMAILS];
    NSArray* emails = [custom valueForKey:EMAILS];

    
    
    NSMutableArray* temp = [NSMutableArray arrayWithCapacity:10];
    for (NSUInteger i = 0; i < [emails count]; i++)
    {
        [temp addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[emails objectAtIndex:i], @"email", nil]];
    }
    [temp addObjectsFromArray:custom_emails];
    return temp;
}
-(IBAction)send:(id)sender
{
    [[AppDelegate sharedInstance].main submitWithDeleteTicket:NO];
}
-(IBAction)add_email:(id)sender
{
    if ([email_address.text length] != 0)
    {
        TicketList* list = [AppDelegate sharedInstance].main.ticketsDataSource;
        NSDictionary* custom = [list.memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
        
        NSMutableArray* custom_emails = [custom valueForKey:CUSTOM_EMAILS];
        if (custom_emails == nil)
        {
            custom_emails = [NSMutableArray arrayWithCapacity:10];
            [custom setValue:custom_emails forKey:CUSTOM_EMAILS];
        }
        
        [custom_emails addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:email_address.text, @"email",[NSNumber numberWithBool:YES],@"changable", nil]];
        [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:self];
    }
}
-(IBAction)cancel:(id)sender
{
    [self removeFromSuperview];
}
- (void)tableView:(UITableView*)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    email_list.selectedIndex = [indexPath row];
}
- (void)viewLoadFromNib:(NSNumber*)param
{
    ticketID = [param longLongValue];
    email_list.ticketID = ticketID;
    email_list.datasource = self;
    [self bind];
}

@end


@implementation EMailList
@synthesize selectedIndex;
@synthesize ticketID;
@synthesize datasource;

- (void)setSelectedIndex:(int)_selectedIndex
{
    selectedIndex = _selectedIndex;
    [tableView reloadData];
}

- (void)delete:(id)sender
{
    TicketList* list = [AppDelegate sharedInstance].main.ticketsDataSource;
    NSDictionary* custom = [list.memoryObject objectForKey:[NSString stringWithFormat:@"%lld", ticketID]];
    
    NSMutableArray* custom_emails = [custom valueForKey:CUSTOM_EMAILS];
    NSArray* emails = [custom valueForKey:EMAILS];
    
    [custom_emails removeObjectAtIndex:self.selectedIndex-[emails count]];
    [[NSNotificationCenter defaultCenter] postNotificationName:DataSourceChangeNotification object:datasource];
    self.selectedIndex = -1;
}
-(void)createSubView:(UITableViewCell*)cell
{    
    UILabel* address = [[UILabel alloc] initWithFrame:CGRectMake(23, 0, 591, 38)];
    [cell.contentView addSubview:address];
    address.backgroundColor = [UIColor clearColor];
    address.tag = 1;
    [address release];
    
    MultiLanguageButton* btn = [[MultiLanguageButton alloc] initWithFrame:CGRectMake(533, 4, 72, 30)];
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_delete_bg.png"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.contentView addSubview:btn];
    btn.tag = 3;
    [btn setTextID:STR_DELETE];
    [btn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [btn release];
    
    cell.contentView.backgroundColor = [UIColor clearColor];
}
-(void)setupNewCell:(UITableViewCell*)cell obj:(id<NSObject>)obj indexPath:(NSIndexPath*)indexPath
{
    NSDictionary* dic = (NSDictionary*)obj;
    UILabel* address = (UILabel*)[cell.contentView viewWithTag:1];
    MultiLanguageButton* btn = (MultiLanguageButton*)[cell.contentView viewWithTag:3];
    address.text = [dic valueForKey:@"email"];
    btn.hidden = self.selectedIndex != [indexPath row];
    if ([dic valueForKey:@"changable"] == nil)
    {
        btn.hidden = YES;
    }
}
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath obj:(id<NSObject>)obj
{
    return 38;
}

@end
